package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.query.DepartmentQuery;
import cn.itsource.pethome.org.service.IDepartmentService;
import cn.itsource.pethome.utils.AjaxResult;
import cn.itsource.pethome.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Department department){
        if (department.getId() == null){
            departmentService.save(department);
        }else {
            departmentService.update(department);
        }
        return AjaxResult.me();
    }

    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        departmentService.delete(id);
        return AjaxResult.me();
    }

    @GetMapping(value = "/{id}")
    public Department selectById(@PathVariable("id") Long id){
        return departmentService.loadById(id);
    }

    @GetMapping
    public List<Department> seleceAll(){
        return departmentService.loadAll();
    }

    @PostMapping
    public PageResult<Department> seleceByPage(@RequestBody DepartmentQuery query){
        return departmentService.selectByPage(query);
    }

    @PatchMapping
    public AjaxResult deletes(@RequestBody Long[] ids){
        departmentService.deletes(ids);
        return AjaxResult.me();
    }

}
